package server

import (
	"server_go/internal/handler"
	"server_go/internal/middleware"
	"server_go/pkg/jwt"
	"server_go/pkg/log"
	"server_go/pkg/server/http"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func NewHTTPServer(
	logger *log.Logger,
	conf *viper.Viper,
	jwt *jwt.JWT,
	userHandler *handler.UserHandler,
	adiminHandler *handler.AdiminHandler,
	usersHandler *handler.UsersHandler,
	tagHandler *handler.TagHandler,
	articleHandler *handler.ArticleHandler,
) *http.Server {
	gin.SetMode(gin.DebugMode)
	s := http.NewServer(
		gin.Default(),
		logger,
		http.WithServerHost(conf.GetString("http.host")),
		http.WithServerPort(conf.GetInt("http.port")),
	)

	s.Use(
		middleware.CORSMiddleware(),
		middleware.ResponseLogMiddleware(logger),
		middleware.RequestLogMiddleware(logger),
	)

	s.POST("/register", userHandler.Register)

	article := s.Group("/article")
	{
		article.Use(middleware.StrictAuth(jwt, logger))
		article.DELETE("deleteArticle/:articleId", articleHandler.DeleteArticle)
	}
	admin := s.Group("/admin")
	{
		admin.GET("ReadArticle", articleHandler.AdminReadArticle)
		admin.GET("getTags", tagHandler.GetTags)
		admin.POST("/login", adiminHandler.Login)
		admin.Use(middleware.StrictAuth(jwt, logger))
		admin.PUT("reastPassword/:userId", adiminHandler.ResetPassword)
		admin.POST("createTag", tagHandler.CreateTag)
		admin.DELETE("deleteTag/:tagId", tagHandler.DeleteTag)
		admin.DELETE("/deleteUser/:userId", adiminHandler.DeleteUser)
		admin.GET("/QueryAllUser", adiminHandler.QueryAllUser)
	}
	users := s.Group("/users")
	{
		users.POST("/createUser", adiminHandler.CreateUser)
		users.POST("/userLogin", usersHandler.Login)
		users.Use(middleware.StrictAuth(jwt, logger))
		users.POST("createArticle", articleHandler.CreateArticle)
		users.GET("/readArticleByuserId/:userId", articleHandler.ReadArticlebyUserId)
	}

	return s
}
